package com.iretailer.report.config;


import com.iretailer.report.web.ReportAuthenticationFilter;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2018/10/10 17:11
 * @Description:
 */
@Configuration
public class ShiroConfig {


    /**
     * ShiroFilterFactoryBean 为Shiro过滤器工厂类
     * 配置一些过滤路径
     *
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        //配置shiro默认登录界面地址，前后端分离中登录界面跳转由端控制，后台仅返回json数据
        //没有登录则调转到这个 url
        shiroFilterFactoryBean.setLoginUrl("/unauth.action");
        // 登录成功后跳转的url(前后端分离项目不需要，登录调整由前端控制)
//        shiroFilterFactoryBean.setSuccessUrl("/index");
        // 未授权url
//        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        //注意过滤器配置顺序 不能颠倒
        // 配置退出过滤器，其中具体的退出代码Shiro已经替我们实现了
//        前后端分离自己实现
//        filterChainDefinitionMap.put("/logout", "logout");
//        filterChainDefinitionMap.put("/user/login.action", "anon");
        filterChainDefinitionMap.put("/business/langDictList.action","anon");
        filterChainDefinitionMap.put("/business/configList.action","anon");
        filterChainDefinitionMap.put("/user/login.action", "anon");
        filterChainDefinitionMap.put("/loginOtherMsg.action", "anon");
        filterChainDefinitionMap.put("/", "anon");
        filterChainDefinitionMap.put("/oauth/**", "anon");
        // 需要认证的url
//        filterChainDefinitionMap.put("/**.action", "authc");
//        filterChainDefinitionMap.put("/*/**.action", "user");
        filterChainDefinitionMap.put("/**/**.action", "basic auth");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
        filters.put("basic auth", new ReportAuthenticationFilter());
        return shiroFilterFactoryBean;
    }

    /**
     * SecurityManager 为shiro安全管理器，管理着所有的Subject
     *
     * @return
     */
    @Bean
    public SecurityManager securityManager() {
        //配置 SecurityManager 并注入 shrioRealm
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(shiroRealm(matcher()));
        securityManager.setRememberMeManager(rememberMeManager());
        SecurityUtils.setSecurityManager(securityManager);
        return securityManager;
    }

    /**
     * 引入自己实现的 ShiroRealm
     *
     * @return
     */
    @Bean
    public ShiroRealm shiroRealm(HashedCredentialsMatcher matcher) {
        ShiroRealm shiroRealm = new ShiroRealm();
        shiroRealm.setCredentialsMatcher(matcher);
        return shiroRealm;
    }

    /**
     * 设置加密算法
     *
     * @return
     */
    @Bean
    public HashedCredentialsMatcher matcher() {
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("md5"); //设置加密算法
        matcher.setHashIterations(1);//设置加密算法的次数
        return matcher;
    }

    /**
     * 定义 remember Me 的 Cookie
     *
     * @return
     */
    public SimpleCookie rememberMeCookie() {
        //设置 cookie 名称 ，对应登录接口
        SimpleCookie cookie = new SimpleCookie("rememberMe");
        // 设置 cookie 的过期时间，单位为秒，这里为一天
        cookie.setMaxAge(3600);
        return cookie;
    }

    /**
     * 设置 Cookie 的管理对象
     *
     * @return
     */
    public CookieRememberMeManager rememberMeManager() {
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        cookieRememberMeManager.setCookie(rememberMeCookie());
        cookieRememberMeManager.setCipherKey(Base64.decode("5AvVhmFLUs0KTA3Kprsdag=="));
        return cookieRememberMeManager;
    }
}
